package com.asambleacristiana.repository.dao;

import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

import com.asambleacristiana.utils.PropertiesLoader;

/**
 * Implementacion del Patron DAO
 * http://java.sun.com/blueprints/corej2eepatterns/
 * Patterns/DataAccessObject.html
 * 
 * @author PBalegno
 * */
public class JdbcDataSource {

	private static JdbcDataSource instance = null;
	private static BasicDataSource dataSource = null;

	/**
	 * Implementacion del patron singleton para poder llamar a un unico
	 * DAOFactory
	 * */
	public static synchronized JdbcDataSource getInstance() {
		if (instance == null) {
			instance = new JdbcDataSource();
		}
		return instance;
	}

	/**
	 * Constructor privado
	 * */
	private JdbcDataSource() {
		dataSource = createSource();

	}

	/**
	 * Metodo privado para la conexion a la BD
	 * */
	private BasicDataSource createSource() {
		Properties config = PropertiesLoader.getFileProperties("jdbc.properties");
		BasicDataSource basicDataSource = new BasicDataSource();
		basicDataSource.setDriverClassName(config.getProperty("jdbc.driverClassName"));
		basicDataSource.setUrl(config.getProperty("jdbc.url"));
		basicDataSource.setUsername(config.getProperty("jdbc.username"));
		basicDataSource.setPassword(config.getProperty("jdbc.password"));
		return basicDataSource;
	}

	public BasicDataSource getDataSource() {
		return dataSource;
	}
	
}
